﻿// JSON C# Class Generator
// http://at-my-window.blogspot.com/?page=json-class-generator

using System;
using Newtonsoft.Json.Linq;
using JsonCSharpClassGenerator;
using TomatoSynchronizer.Store;

namespace TomatoSynchronizer.Entities
{

	public class ProductVariant
	{
		public override bool Equals(object obj)
		{
			if(obj is ProductVariant)
			{
				ProductVariant variant = (ProductVariant)obj;
				if(variant._variants_values_name == _variants_values_name && variant.data.Equals(_data))
				{
					if(variant.variants_groups.Length != _variants_groups.Length)
					{
						return false;
					}
					else
					{
						foreach(VariantGroup groupSrc in variant.variants_groups)
						{
							//we assume there is no mapping for this group in the dest groups
							bool sync = false;
							
							foreach(VariantGroup groupDest in _variants_groups)
							{			
								if(groupSrc.Equals(groupDest))
								{
									//we found a mapping in dest groups
									sync = true;
								}
							}
							
							//no mapping in dest groups, we can conclude that the 2 groups does not contains the same entries
							if(sync == false)
							{
								return false;
							}
						}												
					}
				}
			}
			
			return false;
		}

		public ProductVariant()
		{
		}

		public ProductVariant(string json)
			: this(JObject.Parse(json))
		{
		}

		private JObject __jobject;
		public ProductVariant(JObject obj)
		{
			this.__jobject = obj;
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private int _products_variants_id;
		public int products_variants_id
		{
			get
			{
				if(_products_variants_id == null)
					_products_variants_id = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(__jobject, "products_variants_id"));
				return _products_variants_id;
			}
			set
			{
				_products_variants_id = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private int _default;
		public int Default
		{
			get
			{
				if(_default == null)
					_default = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(__jobject, "default"));
				return _default;
			}
			set
			{
				_default = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private string _variants_values_name;
		public string variants_values_name
		{
			get
			{
				if(_variants_values_name == null)
					_variants_values_name = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "variants_values_name"));
				return _variants_values_name;
			}
			set
			{
				_variants_values_name = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private VariantGroup[] _variants_groups;
		public VariantGroup[] variants_groups
		{
			get
			{
//				if(_variants_groups == null)
//					_variants_groups = (VariantsGroup[])JsonClassHelper.ReadArray<VariantsGroup>(JsonClassHelper.GetJToken<JArray>(__jobject, "variants_groups"), JsonClassHelper.ReadStronglyTypedObject<VariantsGroup>, typeof(VariantsGroup[]));
				return _variants_groups;
			}
			set
			{
				_variants_groups = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private string _variants_values;
		public string variants_values
		{
			get
			{
				if(_variants_values == null)
					_variants_values = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "variants_values"));
				return _variants_values;
			}
			set
			{
				_variants_values = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private VariantData _data;
		public VariantData data
		{
			get
			{
				if(_data == null)
					_data = JsonClassHelper.ReadStronglyTypedObject<VariantData>(JsonClassHelper.GetJToken<JObject>(__jobject, "data"));
				return _data;
			}
			set
			{
				_data = value;
			}
		}
	}
}
